package com.neusoft.resturant.controller;


import com.neusoft.resturant.common.utils.R;
import com.neusoft.resturant.entity.Address;
import com.neusoft.resturant.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("addresses")
public class AddressController extends BaseController {
    @Autowired
    private IAddressService iAddressService;

    @PostMapping("add_new_address")
    public R addNewAddress(HttpSession session, Address address) {
        Integer uid = getuidFromSession(session);
        String username = getUsernameFromSession(session);
        iAddressService.addNewAddress(uid, username, address);
        return new R();
    }

    @GetMapping({"", "/"})
    public R getByUidAddress(HttpSession session) {
        Integer uid = getuidFromSession(session);
        List<Address> address = iAddressService.getByUidAddress(uid);
        return R.success(address);
    }

    @PutMapping("{aid}/set_default")
    public R setDefault(@PathVariable("aid") Integer aid,
                        HttpSession session) {
        iAddressService.setDefault(aid, getuidFromSession(session), getUsernameFromSession(session));
        return new R();
    }

    @DeleteMapping("{aid}/delete")
    public R delete(@PathVariable Integer aid, HttpSession session) {
        iAddressService.delete(aid, getuidFromSession(session), getUsernameFromSession(session));
        return new R();
    }
}
